/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package tdmframe; import java.io.File; import java.util.Collections; import java.util.Vector; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; /** * * @author welcome */ public class TDMFrame extends javax.swing.JFrame { /** * Creates new form TDMFrame */ public TDMFrame() { initComponents(); initMyComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ private void initMyComponents(){ projectChooser = new javax.swing.JFileChooser(); projectChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY); } DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) { String curPath = dir.getPath(); DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath); if (curTop != null) { // should only be null at root curTop.add(curDir); } @SuppressWarnings("UseOfObsoleteCollectionType") Vector ol = new Vector(); String[] tmp = dir.list(); for (int i = 0; i < tmp.length; i++) { ol.addElement(tmp[i]); } Collections.sort(ol, String.CASE_INSENSITIVE_ORDER); File f; @SuppressWarnings("UseOfObsoleteCollectionType") Vector files = new Vector(); // Make two passes, one for Dirs and one for Files. This is #1. for (int i = 0; i < ol.size(); i++) { String thisObject = (String) ol.elementAt(i); String newPath; if (curPath.equals(".")) { newPath = thisObject; } else { newPath = curPath + File.separator + thisObject; } if ((f = new File(newPath)).isDirectory()) { addNodes(curDir, f); } else { files.addElement(thisObject); } } // Pass two: for files. for (int fnum = 0; fnum < files.size(); fnum++) { curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum))); } return curDir; } // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { folderJLabel = new javax.swing.JLabel(); folderField = new javax.swing.JTextField(); browseButton = new javax.swing.JButton(); analyzeButton = new javax.swing.JButton(); scrollPane = new javax.swing.JScrollPane(); piePane = new javax.swing.JScrollPane(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Technical Debt Manager"); setName("tdmFrame"); // NOI18N setPreferredSize(new java.awt.Dimension(1000, 600)); folderJLabel.setText("Choose a project folder:"); folderJLabel.setToolTipText(""); browseButton.setText("Browse"); browseButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseButtonActionPerformed(evt); } }); analyzeButton.setText("Analyze"); analyzeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { analyzeButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(folderJLabel) .addGroup(layout.createSequentialGroup() .addComponent(folderField, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(browseButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(analyzeButton)) .addComponent(scrollPane)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(piePane, javax.swing.GroupLayout.PREFERRED_SIZE, 526, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(folderJLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(folderField) .addComponent(browseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE) .addComponent(analyzeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(piePane, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE) .addComponent(scrollPane)) .addGap(56, 56, 56)) ); pack(); }// </editor-fold> private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if (evt.getSource() == browseButton ) { int returnVal = projectChooser.showOpenDialog(TDMFrame.this); if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) { File file = projectChooser.getSelectedFile(); folderField.setText(file.getAbsolutePath()); } } } private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if( evt.getSource() == analyzeButton ){ File dir = projectChooser.getSelectedFile(); tree = new javax.swing.JTree( addNodes(null, dir)); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e .getPath().getLastPathComponent(); MyPieChart aChart = new MyPieChart( piePane, node.toString() ); } }); scrollPane.getViewport().add(tree); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(TDMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TDMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TDMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TDMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TDMFrame().setVisible(true); } }); } //My Variables private javax.swing.JFileChooser projectChooser; private javax.swing.JTree tree; // Variables declaration - do not modify private javax.swing.JButton analyzeButton; private javax.swing.JButton browseButton; private javax.swing.JTextField folderField; private javax.swing.JLabel folderJLabel; private javax.swing.JScrollPane piePane; private javax.swing.JScrollPane scrollPane; // End of variables declaration }